package com.whz.disruptor.tool;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.whz.disruptor.event.MessageEvent;
import com.whz.generic.spring.SpringUtils;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import com.whz.disruptor.handler.ProductHandler;

/**
 * @author whz
 */
@Slf4j
@UtilityClass
public class DisruptorUtils {
    /**
     * 生产消息,消费端原样消费消息内容
     *
     * @param message 消息体
     */
    public static void product(Object message) {
        SpringUtils.getBean(ProductHandler.class).ifPresent(handler -> {
            handler.accept(message);
            log.debug("produced message:{}", message);
        });
    }

    /**
     * 生产消息,消费端消费经过eventTranslatorOneArg装换后的消息
     *
     * @param message               消息体
     * @param eventTranslatorOneArg 消息转换后加载到 {@link MessageEvent}
     */
    public static void product(Object message, EventTranslatorOneArg<MessageEvent, Object> eventTranslatorOneArg) {
        SpringUtils.getBean(ProductHandler.class).ifPresent(handler -> {
            handler.accept(message, eventTranslatorOneArg);
            log.debug("produced message:{}", message);
        });
    }
}